<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>表格渲染</title>
  </head>
  <body>
    <table border="1" cellspacing="0">
      <thead>
        <tr>
          <th>学号</th>
          <th>姓名</th>
          <th>年龄</th>
          <th>性别</th>
        </tr>
      </thead>
    </table>
    <script>
      var userList = [
        { id: 1, name: 'Jack', age: 18, gender: '男' },
        { id: 2, name: 'Rose', age: 20, gender: '女' },
        { id: 3, name: 'Tom', age: 22, gender: '男' },
        { id: 4, name: 'Jerry', age: 24, gender: '女' },
      ];
      // 获取table
      var tabEl=document.querySelector('table')
      // 在table里面创建tbody
      var tbodyEl=document.createElement('tbody')
      tabEl.append(tbodyEl)
      // 在tbody里面创建tr
      for(var i=0;i<userList.length;i++){
        // 创建的tr
        var trEl=document.createElement('tr')
        // 获取每个对象
        var obj=userList[i]
        console.log(obj);
        // 遍历每个对象
        for(var o in obj){
          // console.log(obj[o]);
          // 创建td
          var tdEl=document.createElement('td')
          // 把每个属性值放到td里
          tdEl.textContent=obj[o]
          // 把每个td放到tr里
          trEl.append(tdEl)
        }
        tbodyEl.append(trEl)

      }
    </script>
  </body>
</html>
